package com.example.cloud.controller.personal;


import com.example.cloud.controller.common.BaseController;
import com.example.cloud.model.dto.Result;
import com.example.cloud.model.personal.MainClass;
import com.example.cloud.service.business.attendance.MainClassService;
import com.example.cloud.utils.BusinessUtil;
import com.example.cloud.utils.RequestUtil;
import com.example.cloud.utils.ResultUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by ygl on 2017/4/9.
 */
@RestController
@RequestMapping("/business/mainClass")
public class MainClassController extends BaseController {

    private static final Logger logger = Logger.getLogger(MainClassController.class);

    @Autowired
    private MainClassService mainClassService;

    @GetMapping("/getCalendar/{month}")
    public Result getCalendar(@PathVariable String month) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        List<List<MainClass>> resultList = null;
        try {
            resultList = mainClassService.findAllByCurrentDate(sdf.parse(month), RequestUtil.getLoginUser());
        }catch (Exception e){
            logger.error("查询主班排班表异常：" + e.getMessage(), e);
            ResultUtil.fail("系统异常，请联系管理员");
        }
        return ResultUtil.success(resultList);
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody MainClass record){
        MainClass mainClass = null;
        try {
            BusinessUtil.appendRecord(record);
            mainClass = mainClassService.saveOrUpdate(record);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ResultUtil.fail("保存失败,请联系管理员");
        }
        return ResultUtil.success(mainClass);
    }
}

